/*
* @Author: q5601
* @Date:   2019-03-31 22:19:15
* @Last Modified by:   q5601
* @Last Modified time: 2019-03-31 22:19:34
*/
/*
* @Author: q5601
* @Date:   2019-03-31 22:19:15
* @Last Modified by:   q5601
* @Last Modified time: 2019-03-31 22:19:34
*/
(function() {
  'use stract';

  var docEl = document.documentElement,
    viewportEl = document.querySelector('meta[name="viewport"]'),
    dpr = window.devicePixelRatio || 1,
    maxWidth = 0,
    minWidth = 320;

  dpr = dpr >= 3 ? 3 : (dpr >= 2 ? 2: 1) // 可能会有小数 判断让它为整数

  docEl.setAttribute('data-dpr', dpr)
  docEl.setAttribute('max-width', maxWidth)
  docEl.setAttribute('min-width', minWidth)

  let scale = 1 / dpr,
    content = `width=device-width, initial-scale=${scale}, maximum-scale=${scale}, minimum-scale=${scale}, user-scalable=no`;

  if (viewportEl) {
    viewportEl.setAttribute('content', content);
  } else {
    viewportEl = document.createElement('meta');
    viewportEl.setAttribute('name', 'viewport');
    viewportEl.setAttribute('content', content);
    document.head.appendChild(viewportEl)
  }

  window.addEventListener('resize', setRemUnit)

  setRemUnit()
  function setRemUnit() {
    let ratio = 18.75;
    let viewWidth = window.innerWidth || docEl.clientWidth;

    if (maxWidth && (viewWidth / dpr > maxWidth)) {
      viewWidth = maxWidth * dpr
    } else if (minWidth && (viewWidth / dpr < minWidth)) {
      viewWidth = minWidth * dpr
    }

    docEl.style.fontSize = viewWidth / ratio + 'px';
  }
})()
